using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace QuanLyThuoc.DAL
{
   public class KetNoiDAL
    {
       
        SqlConnection conn;
        SqlDataAdapter da;
        SqlCommand cmd;
        DataTable dt;
        
        public static SqlConnection Connect()
        {
            string str = @"server=TISA\SQLEXPRESS;database=QuanLyThuoc;uid=sa;pwd=sa";
            //string str = @"Data Source=AIY5VYDFO4GZFBY\SQLEXPRESS;Initial Catalog=QuanLyThuoc;Integrated Security=True";
            SqlConnection con = new SqlConnection(str);
            con.Open();
            return con;
        }
        public  DataTable GetDataTables(string pcQuery, string pcAsName)
        {
            DataTable dtReturn = new DataTable(pcAsName);
            try
            {
                conn = Connect();
                da = new SqlDataAdapter(pcQuery, conn);
                dt = new DataTable(pcAsName);
                da.Fill(dtReturn);
            }
            catch { }
            return dt;
        }
       
       public DataTable GetDataTable(string sql)
       {
         
           try
           {
               conn = Connect();
               da = new SqlDataAdapter(sql, conn);
               dt = new DataTable();
               da.Fill(dt);
           }
           catch { }
           return dt;
       }
        public void ExcuteNonQuery(string sql)
        {
            conn = Connect();
            conn.Open();
            cmd = new SqlCommand(sql, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            conn.Dispose();
        }
        public DataTable Search(string sql)
        {
            conn = Connect();
            conn.Open();
            cmd = new SqlCommand(sql, conn);
            da = new SqlDataAdapter();
            DataTable dt = new DataTable();
            da.SelectCommand = cmd;
            da.Fill(dt);
            return dt;
        }
       public string TangChuoi(string pcNumber)
       {
           string rightString = "", strReturn = "";
           string strDk = "0123456789";
           try
           {
               for (int i = pcNumber.Length - 1; i >= 0; i--)
               {
                   if (strDk.Contains(pcNumber.Substring(i, 1)))
                       rightString = pcNumber.Substring(i, 1) + rightString;
                   else
                       break;
               }

               if (rightString == "")
               {
                   rightString = "0";
                   strReturn = pcNumber + 1;
               }
               else
               {
                   ulong nnumber = ulong.Parse(rightString);
                   nnumber = nnumber + 1;
                   strReturn = nnumber.ToString();
                   strReturn = pcNumber.Substring(0, (pcNumber.Length - rightString.Length)) + strReturn.PadLeft(rightString.Length, '0');
               }
           }
           catch (Exception ex)
           { 
           }
           return strReturn;
       }
    }
}
